package com.blog.common.utils;

import com.blog.common.core.domain.AjaxResult;
import com.blog.common.core.domain.entity.SysUser;
import org.apache.shiro.subject.Subject;


public class UserAuthenticationUtils {

    public static AjaxResult getUsernameFromSubject(Subject subject) {
        if (subject.isAuthenticated()) {
            Object principal = subject.getPrincipal();
            if (principal instanceof SysUser) {
                SysUser user = (SysUser) principal;
                Long userId = user.getUserId();
                return AjaxResult.success("获取用户id成功", userId);
            } else {
                return AjaxResult.error("无法获取用户id，Principal 类型不匹配");
            }
        } else {
            return AjaxResult.error("用户未登录");
        }
    }
}